package com.fh.modules.oms.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.oms.entity.OmsOrderReturn;
import com.fh.modules.oms.feign.FhWechatOrderApi;
import com.fh.modules.oms.service.IOmsOrderReturnService;
import com.fhMall.request.OrderReturnApplyRequest;
import com.fhMall.request.OrderReturnHandleRequest;
import com.fhMall.request.WechatOrderReturnHandleResquest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @Title: Controller
 * @Description: 订单退货
 * @author： code generator
 * @date： 2019-10-18
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("订单退货")
public class OmsOrderReturnController {

    @Autowired
    private IOmsOrderReturnService omsOrderReturnService;




    /**
     * 分页列表查询
     *
     * @param omsOrderReturn
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/oms/omsOrderReturn/list")
    @ApiOperation("分页列表查询")
    @AutoLog(value = "订单退货分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPageList(OmsOrderReturn omsOrderReturn,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                HttpServletRequest req) {
        QueryWrapper<OmsOrderReturn> queryWrapper = new QueryWrapper<OmsOrderReturn>();
        QueryWrapperUtil.initQueryWrapper(queryWrapper, omsOrderReturn);
        Page<OmsOrderReturn> page = new Page<OmsOrderReturn>(pageNo, pageSize);
        IPage<OmsOrderReturn> pageList = omsOrderReturnService.page(page, queryWrapper);
        return Result.ok(pageList);
    }


    @PostMapping(value = "/oms/omsOrderReturn/apply")
    @ApiOperation("申请退货")
    @AutoLog("申请退货")
    public Result apply(@RequestBody OrderReturnApplyRequest orderReturnApplyRequest) {
        omsOrderReturnService.applyOrderReturn(orderReturnApplyRequest);
        return Result.ok("申请退货成功");
    }

    @PostMapping(value = "/oms/omsOrderReturn/handle")
    @ApiOperation("处理申请退货单")
    @AutoLog("处理申请退货单")
    public Result handle(@RequestBody OrderReturnHandleRequest orderReturnHandleRequest) {
        WechatOrderReturnHandleResquest wechatOrderReturnHandleResquest = omsOrderReturnService.handleOrderReturn(orderReturnHandleRequest);

        return Result.ok("拒绝退货成功");
    }

    @PutMapping(value = "/oms/omsOrderReturn/returnSuccess")
    @ApiOperation("订单退货成功，业务处理")
    public void returnSuccess(@RequestParam(name = "outRefundNo") String outRefundNo, @RequestParam(name = "outTradeNo") String outTradeNo, @RequestParam(name = "transactionId") String transactionId){
        omsOrderReturnService.returnSuccess(outRefundNo, outTradeNo, transactionId);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/oms/omsOrderReturn/queryById")
    @ApiOperation("通过id查询")
    @AutoLog(value = "订单退货通过id查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryById(@RequestParam(name = "id", required = true) String id) {
        OmsOrderReturn omsOrderReturn = omsOrderReturnService.getById(id);
        return Result.ok(omsOrderReturn);
    }

}
